package edu.ustb.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import edu.ustb.domain.user.Users;
import edu.ustb.service.user.Impl.UsersServiceImpl;
import edu.ustb.service.user.UsersService;
import edu.ustb.view.JsonData;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

@WebServlet(name = "UserRegisterController", value = "/users/reg")
public class UserRegisterController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain;charset=utf-8");
        PrintWriter out = response.getWriter();
        try {
            //获得json字符串，json字符串-》json对象-》java对象
            BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), "utf-8"));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            System.out.println(sb);

            //字符串转换json对象。
            //JSONObject jsonObject = JSONUtil.parseObj(sb.toString());
            JSONObject jsonObject = JSONObject.parseObject(sb.toString());

            String phoneNum = jsonObject.getString("mobile");
            String passWord = jsonObject.getString("password");
            String randomCode = jsonObject.getString("randomCode");
            String email = jsonObject.getString("email");
            System.out.println(phoneNum);
            System.out.println(passWord);
            System.out.println(randomCode);
            System.out.println(email);

            //判断用户输入的验证码是否正确
            String yoursRandomCode = randomCode;

            if (request.getSession().getAttribute("sessionRandomCode") == null) {
                JsonData result = JsonData.fail("请填写验证码！");
                out.write(JSONObject.toJSONString(result));
                out.flush();
                out.close();
                return;
            }

            String sessionRandomCode = (String) request.getSession().getAttribute("sessionRandomCode");
            if (!yoursRandomCode.equals(sessionRandomCode)) {
                JsonData result = JsonData.fail("验证码错误！");
                out.write(JSONObject.toJSONString(result));
                out.flush();
                out.close();
                return;
            }
            Users users = new Users();
            users.setUserPhone(phoneNum);
            users.setUserPass(passWord);
            users.setUserEmail(email);

            UsersService usersService = new UsersServiceImpl();
            int flag =usersService.register(users);
            if (flag==1) {
                JsonData result = JsonData.success("用户注册成功！");
                out.write(JSONObject.toJSONString(result));
            }
            else if(flag==2){
                JsonData result = JsonData.fail("手机号码已存在，可直接登录！");
                out.write(JSONObject.toJSONString(result));
            }
            else {
                JsonData result = JsonData.fail("用户注册失败！");
                out.write(JSONObject.toJSONString(result));
            }
            out.flush();
            out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            JsonData result = JsonData.fail("程序异常！");
            out.write(JSONObject.toJSONString(result));
            out.flush();
            out.close();
        }
    }
}
